package com.lzq.badminton.security;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        // 设置响应的状态码为 401 Unauthorized
        httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

        // 设置响应体内容，可以是自定义的错误消息
        httpServletResponse.getWriter().write("Authentication failed: " + e.getMessage());

        // 如果需要，可以设置其他响应头信息
        httpServletResponse.setHeader("Content-Type", "application/json");
    }
}

